﻿Public Class Baremes_Screen


    Private _ctrl As Baremes_CTRL
    Private _baremeCourant As Baremes
    Private _flagModif As Boolean
    Private _FlagAjout As Boolean

    Public Sub New(ByVal ctrl As Baremes_CTRL)
        InitializeComponent()

        _ctrl = ctrl
        _baremeCourant = New Baremes
        _flagModif = False
    End Sub




    ''' <summary>
    ''' Initialisation des contrôleurs
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub Baremes_Screen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'RafraichirListeBareme()
        'CMBselectiontypeBaremes.DataSource = _ctrl.getListeCategorie
        With BarBouton1
            .PnlNavigation.Visible = True
            '    With .BtnPremier
            '        .Visible = True
            '        .Enabled = False
            '    End With
            '    With .BtnPrecedent
            '        .Visible = True
            '        .Enabled = False
            '    End With
            '    With .BtnSuivant
            '        .Visible = True
            '        .Enabled = False
            '    End With
            '    With .BtnDernier
            '        .Visible = True
            '        .Enabled = False
            '    End With
            .PnlGestionDonnées.Visible = True
            '    With .BtnSupprimer
            '        .Visible = True
            '        .Enabled = True
            '    End With
            '    With .BtnAjouter
            '        .Visible = True
            '        .Enabled = True
            '    End With
            '    With .BtnAnnuler
            '        .Visible = True
            '        .Enabled = False
            '    End With
            '    With .BtnArchivage
            '        .Visible = True
            '        .Enabled = True
            'End With
            .ToolTipBouton.SetToolTip(.BtnArchivage, "Mettre à jour les tarifs pour un acte")
        End With
        ModeAffichage(actionEnum.Annuler)
    End Sub

    ''' <summary>
    ''' Reactualisation de la datagridview servant à l'affichage des baremes
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub RafraichirListeBareme()
        CMBselectiontypeBaremes.DataSource = Nothing
        CMBselectiontypeBaremes.DataSource = _ctrl.getListeCategorie
        DGVbaremes.DataSource = Nothing
        DGVbaremes.DataSource = _ctrl.getListeBareme
        DGVbaremes.Refresh()
    End Sub

    ''' <summary>
    ''' Gestion du mode d'affichage en fonction des boutons cliqués
    ''' </summary>
    ''' <param name="mode"></param>
    ''' <remarks></remarks>
    Private Sub ModeAffichage(ByVal mode As actionEnum)
        Select Case mode
            Case actionEnum.Ajouter
                RADTarifFixe.Checked = True
                DGVbaremes.Enabled = False
                PNLAjout.Visible = True
                With BarBouton1
                    .PnlNavigation.Enabled = False
                    .BtnAnnuler.Enabled = True
                    .BtnEnregistrer.Enabled = False
                    .BtnAjouter.Enabled = False
                    .BtnArchivage.Enabled = False
                    .BtnSupprimer.Enabled = False
                End With
                LBLselectiontypeBaremes.Text = "Choissisez le type d'acte :"
                CMBselectiontypeBaremes.DataSource = Nothing
                CMBselectiontypeBaremes.DataSource = _ctrl.getListeCategorie(True)
                PNLSelectionTypeTarif.Enabled = True
                PNLTarif.Enabled = True
                With TXTmini
                    .Enabled = True
                    .Text = ""
                End With
                With TXTmaxi
                    .Enabled = True
                    .Text = ""
                End With
                _FlagAjout = True
            Case actionEnum.Annuler, actionEnum.Enregistrer
                _FlagAjout = False
                _flagModif = False
                With ErrorProviderBaremes
                    .SetError(TXTCodeGroupement, "")
                    .SetError(TXTLibelle, "")
                    .SetError(TXTmini, "")
                    .SetError(TXTmaxi, "")
                End With
                CMBselectiontypeBaremes.Enabled = True
                DGVbaremes.Enabled = True
                RafraichirListeBareme()
                With BarBouton1
                    .PnlNavigation.Enabled = True
                    .BtnAnnuler.Enabled = False
                    .BtnAjouter.Enabled = True
                    .BtnEnregistrer.Enabled = False
                    .BtnArchivage.Enabled = True
                    With .BtnSupprimer
                        If DGVbaremes.DataSource.Count > 0 Then
                            .Enabled = True
                        Else
                            .Enabled = False
                        End If
                    End With
                End With
                PNLAjout.Visible = False
                LBLselectiontypeBaremes.Text = "Selectionnez la catégorie de barèmes à afficher :"
                TXTmaxi.Enabled = False
                TXTmini.Enabled = False
                PNLSelectionTypeTarif.Enabled = False
                PNLTarif.Enabled = False
                DGVbaremes_SelectionChanged(Me, New EventArgs)
            Case actionEnum.Archiver
                CMBselectiontypeBaremes.Enabled = False
                DGVbaremes.Enabled = False
                With BarBouton1
                    .PnlNavigation.Enabled = False
                    .BtnAnnuler.Enabled = True
                    .BtnEnregistrer.Enabled = False
                    .BtnAjouter.Enabled = False
                End With
                PNLSelectionTypeTarif.Enabled = True
                PNLTarif.Enabled = True
                With TXTmini
                    .Enabled = True
                    .Text = ""
                End With
                With TXTmaxi
                    .Enabled = True
                    .Text = ""
                End With
                _flagModif = True

        End Select
    End Sub

    ''' <summary>
    ''' Gestion des évènements Action générés par la barre de boutons
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BarBouton1_Action(ByVal sender As Object, ByVal e As ActionEventArgs) Handles BarBouton1.Action
        Select Case e.Action
            Case actionEnum.Archiver
                ModeAffichage(actionEnum.Archiver)
            Case actionEnum.Annuler
                ModeAffichage(actionEnum.Annuler)
            Case actionEnum.Enregistrer
                Dim prixMini, prixMaxi As Decimal
                If _flagModif Then
                    If isSaisieOk(prixMini, prixMaxi) Then
                        _ctrl.EnregistrerModif(DGVbaremes.SelectedRows(0).DataBoundItem, prixMini, prixMaxi, RADTarifFixe.Checked)
                        ModeAffichage(actionEnum.Annuler)
                    End If
                End If
                If _FlagAjout Then
                    Dim typeActe As String = ""
                    Dim vaccin As New Vaccins
                    vaccin = Nothing
                    If isSaisieAjoutOk(prixMini, prixMaxi, TXTCodeGroupement.Text, TXTLibelle.Text, typeActe) Then
                        _FlagAjout = False
                        If typeActe = "VACC" Then
                            Dim dlg As New SaisieNouveauVaccin_DLG(_ctrl.getSaisieNouveauVaccin_CTRL)
                            Do Until dlg.ShowDialog() = Windows.Forms.DialogResult.OK
                                vaccin = _ctrl.getVaccinEnregistre
                            Loop
                        End If
                        _ctrl.EnregistrerNouveau(TXTCodeGroupement.Text, typeActe, TXTLibelle.Text, RADTarifFixe.Checked, prixMini, prixMaxi, vaccin)
                        ModeAffichage(actionEnum.Annuler)
                    End If
                End If
            Case actionEnum.Ajouter
                ModeAffichage(actionEnum.Ajouter)
            Case actionEnum.Supprimer
                _ctrl.SupprimerBareme(CType(DGVbaremes.SelectedRows(0).DataBoundItem, Baremes))
                ModeAffichage(actionEnum.Annuler)
        End Select
    End Sub



    Private Function isSaisieAjoutOk(ByRef prixMini As Decimal, _
                                     ByRef prixMaxi As Decimal, _
                                     ByVal codeGroupement As String, _
                                     ByVal libelle As String, _
                                     ByRef typeActe As String) As Boolean
        isSaisieAjoutOk = True
        isSaisieAjoutOk = isSaisieOk(prixMini, prixMaxi)
        With ErrorProviderBaremes
            .SetError(TXTCodeGroupement, "")
            .SetError(TXTLibelle, "")
            If codeGroupement.Length <> 3 Then
                .SetError(TXTCodeGroupement, "Le code groupement doit comporter 3 caractères")
                isSaisieAjoutOk = False
            End If
            If TXTLibelle.Text.Length > 30 Then
                .SetError(TXTLibelle, "Libellé trop long (30 caractères au maximum)")
                isSaisieAjoutOk = False
            ElseIf TXTLibelle.Text.Length = 0 Then
                .SetError(TXTLibelle, "Veuillez saisir un libellé pour l'acte")
                isSaisieAjoutOk = False
            End If
            With CMBselectiontypeBaremes
                If .SelectedItem = .Items(0) Then
                    typeActe = ""
                    Dim msg As String = "Veuillez saisir le nouveau code du type d'acte (4 caractères)"
                    Do
                        typeActe = InputBox(msg)
                        msg = "Saisie incorrecte." & vbCrLf & msg
                    Loop Until typeActe.Length = 4
                Else
                    typeActe = .SelectedItem
                End If
            End With
        End With
    End Function

    Private Function isSaisieOk(ByRef prixMini, ByRef prixMaxi) As Boolean
        isSaisieOk = True
        With ErrorProviderBaremes
            .SetError(TXTmini, "")
            .SetError(TXTmaxi, "")
            If Not Decimal.TryParse(TXTmini.Text, prixMini) Then
                .SetError(TXTmini, "Saisie non valide")
                isSaisieOk = False
            Else
                prixMini = TXTmini.Text
            End If
            If RADTarifVariable.Checked Then
                If Not Decimal.TryParse(TXTmaxi.Text, prixMaxi) Then
                    .SetError(TXTmaxi, "Saisie non valide")
                    isSaisieOk = False
                Else
                    prixMaxi = TXTmaxi.Text
                    If prixMini > prixMaxi Then
                        .SetError(TXTmaxi, "Le tarif maximum doit être supérieur au tarif minimum")
                        isSaisieOk = False
                    End If
                End If
            End If
        End With
    End Function

    ''' <summary>
    ''' Procédure de controle de navigation
    ''' envoi le nouvel index au controleur en fonction du bouton cliqué
    ''' et de paramètres triviaux
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BarBouton1_Navigation(ByVal sender As Object, ByVal e As navigationEventArgs) Handles BarBouton1.Navigation
        Select Case e.navigation
            Case navigationEnum.Premier
                _ctrl.indexCourant = 0
                DGVbaremes.Rows(_ctrl.indexCourant).Selected = True
            Case navigationEnum.Precedent
                _ctrl.indexCourant -= 1
                DGVbaremes.Rows(_ctrl.indexCourant).Selected = True
            Case navigationEnum.Suivant
                _ctrl.indexCourant += 1
                DGVbaremes.Rows(_ctrl.indexCourant).Selected = True
            Case navigationEnum.Dernier
                _ctrl.indexCourant = DGVbaremes.Rows.Count - 1
                DGVbaremes.Rows(_ctrl.indexCourant).Selected = True

        End Select
    End Sub



    Private Sub DGVbaremes_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGVbaremes.SelectionChanged
        If DGVbaremes.SelectedRows.Count > 0 Then
            _baremeCourant = DGVbaremes.SelectedRows(0).DataBoundItem
            _ctrl.indexCourant = DGVbaremes.Rows.IndexOf(DGVbaremes.SelectedRows(0))
            With BarBouton1
                .BtnPrecedent.Enabled = False
                .BtnPremier.Enabled = False
                .BtnSuivant.Enabled = False
                .BtnDernier.Enabled = False
                If _ctrl.indexCourant > 0 Then
                    .BtnPrecedent.Enabled = True
                    .BtnPremier.Enabled = True
                End If
                If _ctrl.indexCourant < DGVbaremes.DataSource.Count - 1 Then
                    .BtnSuivant.Enabled = True
                    .BtnDernier.Enabled = True
                End If
            End With
            With CType(DGVbaremes.SelectedRows(0).DataBoundItem, Baremes)
                If .TarifFixe = 0 Then
                    RADTarifVariable.Checked = True
                    TXTmini.Text = .TarifMini
                    TXTmaxi.Text = .TarifMaxi
                Else
                    RADTarifFixe.Checked = True
                    TXTmini.Text = .TarifFixe

                End If
            End With
        End If

    End Sub


    Private Sub CMBselectiontypeBaremes_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CMBselectiontypeBaremes.SelectedIndexChanged
        If Not _FlagAjout Then
            _ctrl.setListeBareme(CMBselectiontypeBaremes.SelectedItem)
            RafraichirListeBareme()
        End If

    End Sub

    Private Sub RADTarifFixe_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RADTarifFixe.CheckedChanged
        Select Case RADTarifFixe.Checked
            Case True
                RADTarifFixe.Checked = True
                LBLtarifMini.Text = "Tarif :"
                LBLtarifMaxi.Text = ""
                TXTmaxi.Visible = False
            Case False
                RADTarifVariable.Checked = True
                LBLtarifMini.Text = "Tarif minimum :"
                LBLtarifMaxi.Text = "Tarif maximum :"
                TXTmaxi.Visible = True
        End Select
    End Sub

    Private Sub TXTmini_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXTmini.TextChanged
        If _flagModif Or _FlagAjout Then
            BarBouton1.BtnEnregistrer.Enabled = True
        End If
    End Sub

    Private Sub BTNmisajourAUTO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNmisajourAUTO.Click

        _ctrl.MaJDepuisXML()
        RafraichirListeBareme()
    End Sub
End Class